package hp.com.servlet.CartDataServlet;

import hp.com.entity.CartData;
import hp.com.entity.Users;
import hp.com.service.CartDataService;
import hp.com.service.impl.CartDataServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

@WebServlet("/Cart")
public class BuyCarShowServlet extends HttpServlet {
    private CartDataService cartDataService = new CartDataServiceImpl();

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request,response);
    }

    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        try {
            //判断是否登录且获取对象
            HttpSession session = req.getSession();
            if (session.getAttribute("userSession") == null) {
                req.getRequestDispatcher(req.getContextPath() + "/Login").forward(req, resp);
            }else{
            //获取购物车商品List
            Users user = (Users)session.getAttribute("userSession");
            req.setAttribute("userSession",user);
            List<CartData> buy = cartDataService.BuyCarShow(String.valueOf(user.getId()));
            req.setAttribute("Buy",buy);
            req.getRequestDispatcher(req.getContextPath() +"ShopPage/Cart.jsp").forward(req,resp);
        }
        }catch (Exception exception) {
            exception.printStackTrace();
        }
    }
}
